//
//  EWMineViewController.m
//  haveSomethingToEat
//
//  Created by 陈宁权 on 2017/4/8.
//  Copyright © 2017年 陈 宁权. All rights reserved.
//

#import "EWMineViewController.h"
#import "EWUserInfoTableViewCell.h"
#import "EWMJDIYHeader.h"
#import "EWUserInfoSettingViewController.h"

@interface EWMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) EWUserInfoTableViewCell *userInfoCell;
@end

@implementation EWMineViewController
{
    UIImageView *navBarHairlineImageView;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    navBarHairlineImageView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:133/255.0 green:32/255.0 blue:133/255.0 alpha:1];
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    //配置导航栏
    [self configNav];
    //配置tableview
    [self configTableView];
    //配置观察者更新用户信息
    [self configNotification];
    //配置上拉刷新控件
    EWMJDIYHeader *header = [EWMJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    self.tableView.mj_header = header;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)configNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userIconChanged) name:NSNOTIFICATION_USERICON_CHANGED object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userNameChanged) name:NSNOTIFICATION_USERNAME_CHANGED object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(phoneNumberChanged) name:NSNOTIFICATION_PHONENUMBER_CHANGED object:nil];
}

- (void)userIconChanged
{
    _userInfoCell.userPortriatUri = [RCIM sharedRCIM].currentUserInfo.portraitUri;
}

- (void)userNameChanged
{
    _userInfoCell.userNameText = [RCIM sharedRCIM].currentUserInfo.name;
}

- (void)phoneNumberChanged
{
    _userInfoCell.phoneNumberText = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userMobile"] stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
}

- (void)configNav
{
    [self.navigationItem setTitle:@"我的"];
    
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    settingBtn.frame = CGRectMake(0, 0, 16, 16);
    [settingBtn setBackgroundImage:[UIImage imageNamed:@"setting_up"] forState:UIControlStateNormal];
    [settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
}

- (void)configTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    _userInfoCell = [EWUserInfoTableViewCell cellWithTableView:self.tableView];
    _userInfoCell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.tableView.tableHeaderView = _userInfoCell;
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.view.frame.size.width, 85 * KWidth_Scale);
    
    //给tableHeaderView添加点击事件
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(settingUserInfo)];
    [self.tableView.tableHeaderView addGestureRecognizer:tapGesturRecognizer];
    
    [self.view addSubview:self.tableView];
}

-(void)headerRefresh
{
    [self.tableView.mj_header endRefreshing];
}

- (void)settingUserInfo
{
    //跳转到个人信息设置页面
    EWUserInfoSettingViewController *userInfoSettingVC = [[EWUserInfoSettingViewController alloc] init];
    [self.navigationController pushViewController:userInfoSettingVC animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0){
        return 85 * KWidth_Scale;
    }
    else{
        return 50 * KWidth_Scale;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)settingBtnClick
{
    NSLog(@"点击了设置按钮");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
